﻿using System;
using System.Xml;

namespace DBUtility
{
    /// <summary>
    /// Summary description for ReadWriteConfig.
    /// </summary>
    class ConfigOperator
    {
        public static void setConfigPara(string strPara, string strText)
        {
            //string strErr;
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                if (path.IndexOf("bin") > -1)
                {
                    //windows服务的连接
                    path = path + "\\Connection.config";
                }
                else
                {
                    //web连接
                    path = "\\bin\\Connection.config";
                }
                //string path = AppDomain.CurrentDomain.BaseDirectory + "\\Connection.config";
                XmlDocument xd = new XmlDocument();
                xd.Load(path);

                foreach (XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
                {
                    if (xn1.Attributes["key"].Value == strPara)
                    {
                        if (xn1.Attributes["value"].Value != strText)
                        {
                            xn1.Attributes["value"].Value = strText;
                        }
                        break;
                    }
                }
                //保存web.config 
                xd.Save(path);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static string getConfigPara(string strKey)
        {
            string strValue = "";
            try
            {
                //string path; 
                //if (System.Environment.CurrentDirectory==AppDomain.CurrentDomain.BaseDirectory)
                //{
                string path = AppDomain.CurrentDomain.BaseDirectory;
                if (path.IndexOf("bin") > -1)
                {
                    //windows服务的连接
                    path = path + "Connection.config";
                }
                else
                {
                    //web连接
                    path = path + "bin\\Connection.config";
                }
                //}
                //else
                //{
                //    path = AppDomain.CurrentDomain.BaseDirectory + "\\bin\\Connection.config";
                //}
                XmlDocument xd = new XmlDocument();
                xd.Load(path);

                foreach (XmlNode xn1 in xd.SelectNodes("/configuration/appSettings/add"))
                {
                    if (xn1.Attributes["key"].Value == strKey)
                    {
                        strValue = xn1.Attributes["value"].Value;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return strValue;

        }
    }
}



